package com.nb82.core;

import java.lang.reflect.Method;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Handler.Callback;
import android.os.Message;

import com.google.gson.Gson;
import com.nb82.entity.Task;

/**
 * <b> ����Ҫ���±�д����ִ��ģ�� </b> ��һ���汾Ҫ��дһ���ػ��̣߳�������������м��
 * 
 * @author cxd
 * 
 */
public class KennerControll {
	private TaskListener listener;
	private Context ctx;//APP
	private Gson gson;
	public TaskListener getListener() {
		return listener;
	}

	public void setListener(TaskListener listener) {
		this.listener = listener;
	}

	static {
		executorService = new ScheduledThreadPoolExecutor(2);
	}
	private static KennerControll kenner;

	private KennerControll() {
		gson = new Gson();
	}

	public static KennerControll getInstance() {
		return kenner != null ? kenner : new KennerControll();
	}

	private static ExecutorService executorService;

	// public void doTaskQueueInOneThread(Queue<Task> queue) {
	// executorService.execute(new DoTask(queue));
	// }
	public void doTaskQueue(Queue<Task> queue) {
		while (!queue.isEmpty()) {
			doTask(queue.poll());
		}
	}

	public void doTask(Task task) {
		if (task.getListener() == null) {
			task.setListener(getListener());
		}
		task.startTime = System.currentTimeMillis();
		task.getListener().onStart(task);
		executorService.execute(new DoTask(task));
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}

	class DoTask implements Runnable {
		private Task task;
		private Queue<Task> queue;

		public DoTask(Task task) {
			this.task = task;
		}

		public DoTask(Queue<Task> queue) {
			this.queue = queue;
		}

		@Override
		public void run() {
			if (queue != null) {
				while (!queue.isEmpty()) {
					invoke(queue.poll());
				}
			} else {
				if (task != null) {
					invoke(task);
				}
			}
		}

		private synchronized void invoke(Task task) {
			try {
				Method method = task.clazz.getMethod(task.method,
						task.getClass());
				Object obj = method.invoke(task.obj == null ? task.clazz.newInstance() : task.obj, task);
				if (task.handler != null) {
					Message msg = task.handler.obtainMessage();
					msg.what = task.taskId;
					msg.obj = obj;
					task.handler.sendMessage(msg);
				} else if (task.action != null && !task.action.equals("")
						&& ctx != null ) {
					
					Intent intent = new Intent(task.action);
					if ( obj instanceof String) {
						intent.putExtra("json", obj.toString());
					}else{
						if (gson == null ) {
							gson = new Gson();
						}
						intent.putExtra("json", gson.toJson(obj));
					}
					ctx.sendBroadcast(intent);
				}
				if (task.getListener() != null) {
					task.params.put("obj", obj);
					task.completeTime = System.currentTimeMillis();
					task.getListener().onComplete(task);
				}
			} catch (Exception e) {
				e.printStackTrace();
				if (task.getListener() != null) {
					task.completeTime = System.currentTimeMillis();
					task.getListener().onError(task, e);
				}
			}
		}

	}

	/**
	 * 任务监听器
	 * 
	 * @author oopp1990
	 * 
	 */
	public interface TaskListener {
		public void onStart(Task task);

		public void onComplete(Task task);

		public void onError(Task task, Exception e);
	}

}
